//
//  PPKeyWordsTableViewController.m
//  pet
//
//  Created by 陈无忌 on 2/1/15.
//  Copyright (c) 2015 whalefin. All rights reserved.
//

#import "PPKeyWordsTableViewController.h"
#import "PPSystemConfiguraton.h"

#import "PPSubTableViewCell.h"
#import "PPsubOtherTableViewCell.h"


@interface PPKeyWordsTableViewController ()<UIAlertViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) PPSystemConfiguraton *config;

@property (nonatomic) BOOL showMore;
@end

@implementation PPKeyWordsTableViewController

- (PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.showMore = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!self.showMore) {
        return self.nowArray.count + 1;
    }else{
        return self.nowArray.count + 1 + self.config.keywordArray.count + 1;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.nowArray.count) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nowKeywordCell"];
        cell.textLabel.text = @"添加新标签";
        return cell;
    }else{
        if (indexPath.row <= self.nowArray.count) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nowKeywordCell"];
            NSString *keyword = [self.nowArray objectAtIndex:indexPath.row];
            cell.textLabel.text = keyword;
            return cell;
        }else if (indexPath.row < self.nowArray.count + self.config.keywordArray.count + 1){
            //加载展开列
            PPSubTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subCell"];
            NSString *keyword = [self.config.keywordArray objectAtIndex:indexPath.row - self.nowArray.count - 1];
            cell.systemKeyword.text = keyword;
            [cell.addButton addTarget:self action:@selector(addKeyword:) forControlEvents:UIControlEventTouchUpInside];
            cell.addButton.tag = indexPath.row - self.nowArray.count - 1;
            return cell;
        }else{
            //其他列
            PPsubOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subOtherCell"];
            cell.keywordTF.delegate = self;
            [cell.addButton addTarget:self action:@selector(addOtherKeyword:) forControlEvents:UIControlEventTouchUpInside];
            cell.addButton.tag = indexPath.row;
            return cell;
        }
        
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.nowArray.count) {
        return YES;
    }
    return NO;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.nowArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
#pragma mark - Table View delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.nowArray.count) {
        //展开子列表
        if (self.showMore) {
            self.showMore = NO;
        }else{
            self.showMore = YES;
        }
        [self.tableView reloadData];
    }
}
#pragma mark - TextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - cell button method
- (void)addKeyword:(UIButton *)sender
{
    [self.nowArray addObject:[self.config.keywordArray objectAtIndex:sender.tag]];
    self.showMore = NO;
    [self.tableView reloadData];
}

- (void)addOtherKeyword:(UIButton *)sender
{
    NSIndexPath *index = [NSIndexPath indexPathForRow:sender.tag inSection:0];
    PPsubOtherTableViewCell *cell = (PPsubOtherTableViewCell *)[self.tableView cellForRowAtIndexPath:index];
    if (cell.keywordTF.text.length > 0) {
        [self.nowArray addObject:cell.keywordTF.text];
        self.showMore = NO;
        [self.tableView reloadData];
    }else{
        [PPHUD hudShowWithString:@"请输入标签" andView:self.view];
    }
    
}
@end
